package com.gy.hsim.bservice.framework.mq;

import java.io.IOException;

import com.gy.hsim.bservice.framework.bean.SystemConfig;
import com.gy.hsim.bservice.framework.spring.SpringUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConnectionParameters;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownListener;
import com.rabbitmq.client.ShutdownSignalException;

/**
 * 工具类
 * 
 * @category 工具类
 * @className ConsumerUtil
 * @description 工具类
 * @author Lee
 * @createDate 2014-15-08
 * @version v1.0
 */
public class MQConnection implements ShutdownListener {

	private static Connection connection = null;
//	private static Channel channel = null;
 
	private static SystemConfig config;
	

	public static final String MQ_CHANNEL_COUNT =  "mq.channel.count";

	/**
	 * 获取连接
	 * 
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @param host
	 *            ip地址
	 * @param port
	 *            端口号
	 * @return Connection
	 */
	private static Connection getConnection(String username, String password,
			String host, String port) {
		if (null == connection || !connection.isOpen()) {
			ConnectionParameters parameters = new ConnectionParameters();
			parameters.setUsername(username);
			parameters.setPassword(password);
			parameters.setVirtualHost("/");
			
			// 默认情况下使用用户guest/guest，vhost=/
			ConnectionFactory factory = new ConnectionFactory(parameters);
			try {
				// 获取connection
				connection = factory
						.newConnection(host, Integer.parseInt(port));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return connection;
	}

	/**
	 * 获取Channel
	 * 
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @param host
	 *            ip地址
	 * @param port
	 *            端口号
	 * @return Channel
	 */
	protected static Channel getChannel(String exchange,String queue,String routerKey) throws IOException {
		
		config = (SystemConfig)SpringUtil.getBean("system");
		
		getConnection(config.getMqUserName(), config.getMqPasswd(),config.getMqHostaddr(),config.getMqPort());
		
//		if (channel == null || !channel.isOpen()){
			Channel channel = connection.createChannel();
			
		    channel.exchangeDeclare(exchange, "direct");
		    channel.queueBind(queue, exchange,routerKey);
		    
//		}

		return channel;

	}
	
	/**
	 * 获取Channel
	 * @param username
	 * @param password
	 * @param host
	 * @param port
	 * @return
	 * @throws IOException
	 */
	protected static Channel createChannel(String username,String password,String host,String port) throws IOException {
		
		getConnection(username, password,host,port);

		return connection.createChannel();

	}
	

	/**
	 * 关闭channel和connection
	 * 
	 * @param channel
	 * @return
	 */
	protected static void close(Channel channel) {
		try {
			channel.close();
			connection.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取channel
	 * 
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @param host
	 *            IP地址
	 * @param port
	 *            端口号
	 * @param queueName
	 *            队列名称
	 * @return consumer
	 */
	public static QueueingConsumer getConsumerChannel(String username,
			String password, String host, String port, String queueName)
			throws java.io.IOException, java.lang.InterruptedException {

		Channel channel = getConnection(username, password, host, port)
				.createChannel();
		channel.queueDeclare(queueName, true);
		// 配置好获取消息的方式
		QueueingConsumer consumer = new QueueingConsumer(channel);
		channel.basicConsume(queueName, true, consumer);

		return consumer;
	}

	@Override
	public void shutdownCompleted(ShutdownSignalException arg0) {
		// TODO Auto-generated method stub

	}

}
